package org.example.seata.bs.auth.handler.code;

import cn.hutool.core.collection.CollUtil;
import org.example.seata.common.core.util.ApiResp;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

/**
 * @author hzq
 * @date 2021/12/23 13:19
 */
@Component
public class SendCodeHandlerChain implements SendCodeHandler {

    private final List<AbstractSendCodeHandler> sendCodeHandlers;

    public SendCodeHandlerChain(@Nullable List<AbstractSendCodeHandler> sendCodeHandlers) {
        this.sendCodeHandlers = CollUtil.isNotEmpty(sendCodeHandlers) ? sendCodeHandlers : Collections.emptyList();
    }

    @Override
    public ApiResp<Object> sendCode(String principal, String handlerType) {
        for (AbstractSendCodeHandler sendCodeHandler : sendCodeHandlers) {
            if (sendCodeHandler.supportType(handlerType)) {
                return sendCodeHandler.sendCode(principal, handlerType);
            }
        }
        return ApiResp.ofErrorMessage(String.format("发送验证失败，不支持【%s】类型验证码发送", handlerType));
    }
}
